<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <div class="right-panel-search">
                    <div class="search-input">商品类别</div>
                    <el-select v-model="search.cate_id" @change="typeChange" filterable placeholder="请选择商品类别" clearable>
                        <el-option v-for="item in proType" :key="item.id" :label="item.title" :value="item.id" />
                    </el-select>
                    <div class="search-input">商品款式</div>
                    <el-select v-model="search.style_id" filterable placeholder="请选择商品款式" clearable>
                        <el-option v-for="item in styleList" :key="item.id" :label="item.title" :value="item.id" />
                    </el-select>
                    <el-input v-model="search.keyword" placeholder="工厂货号/SKC/SPU" clearable @change="handleKeyDown" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
                 <!-- :params="query" -->
                <el-table-column type="selection"  align="center" width="50"></el-table-column>
                <el-table-column label="商品编码" prop="goods_code" align="center" width="200"></el-table-column>
                <el-table-column label="商品信息" prop="sample_clothing" align="center" width="350">
                    <template #default="scope">
                        <div class="shopInfo">
                            <el-image
                                class="img"
                                :src="scope.row.goods_image"
                                :preview-src-list="scope.row.goods_image_item"
                                :initial-index="0"
                                fit="cover"
                                preview-teleported="true"
                                hide-on-click-modal
                                />
                            <div class="right">
                                <div>工厂货号：{{scope.row.factory_number}}</div>
                                <div>样衣尺码：{{scope.row.sample_size}}</div>
                                <!-- <div>SKC：{{scope.row.skc}}</div> -->
                                <div>颜色：{{scope.row.colour}}</div>
                                <div v-if="scope.row.sizeCate">商品类别：{{scope.row.sizeCate.title}}</div>
                                <div>商品款式：{{scope.row.style_name}}</div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="工序" prop="color_type" align="center">
                    <template #default="scope">
                        <div style="display:flex;align-items:center;justify-content: center;">
                            <div>
                                <div v-for="(item,index) in scope.row.goodsProcess" :key="index" style="display:flex;align-items:center;justify-content: center;margin-bottom: 8px;">
                                    <div style="width: 120px;text-align-last: justify;margin-right:15px"> {{item.process_name}} - {{item.two_process_name}} </div>
                                    <el-input v-model="item.price" clearable style="width:100px" />
                                    <el-select v-model="item.unit" clearable style="width: 150px" placeholder="请选择">
                                        <el-option label="元/打" :value="1" />
                                        <el-option label="元/件" :value="2" />
                                    </el-select>
                                </div>
                            </div>
                            <el-button v-if="scope.row.goodsProcess.length" type="primary" @click="update(scope.row)" style="margin-left:10px">确定</el-button>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="135">
                    <template #default="scope">
                        <el-button-group style="display:flex;flex-direction:column">
                            <el-button text type="warning" size="small" @click="table_calc(scope.row, scope.$index)" v-auth="'salary.calc'">计算工资</el-button>
                            <el-button text type="primary" size="small" @click="table_record(scope.row, scope.$index)" v-auth="'salary.record'">价格记录</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
    <save v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable :query="{id:this.recordId}"></save>
</template>

<script>
import Save from './save.vue'
export default {
	name: 'user',
	components: {Save},
	data() {
		return {
			dialog: {
				save: false,
			},
			group: [],
			apiObj: this.$API.customer.processPrice,
			selection: [],
			search: {
				cate_id: null,
				style_id: null,
				keyword: null,
			},
            printArr:'',
            proType:[], //商品类别
            styleList:[], //商品款式
            customerList:[],//客户
            query:{
                color_type:1
            },
            recordId:"",
		}
	},
	mounted() {
        this.getType()
        this.getStyle()
        this.getKeHu()
	},
	methods: {
        // 更新工序价格
        async update(row){
            let process_data = []
            row.goodsProcess.map(item=>{
                process_data.push({
                    price:item.price,
                    id:item.id,
                    unit:item.unit,
                    process_id:item.process_id,
                    two_process_id:item.two_process_id,
                })
            })
            let resData = {goods_id:row.goods_id,factory_number:row.factory_number,is_suit:row.is_suit,process_data:process_data}
			var res = await this.$API.produce.updatePrice.post(resData);
			if (res.code == 1) {
                this.$refs.table.refresh()
                this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
        },
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'set') {
				this.$refs.table.refresh()
			}
            this.printArr = ''
		},
        printChange(e){
            if(e==1){
                this.dialog.print = true
                this.$nextTick(() => {
                    this.$refs.printDialog.open('gong').setData(this.selection)
                })
            }
        },
        selectionChange(selection) {
			this.selection = selection;
		},
        // 商品类别
        async getType(){
            var res = await this.$API.all.type.get();
            this.proType = res.data;
        },
        // 商品款式
        async getStyle(){
            var res = await this.$API.goods.getAllGoodsStyle.get();
            this.styleList = res.data;
        },
        // 获取客户
        async getKeHu(){
            var res = await this.$API.all.customer.get();
            this.customerList = res.data;
        },
        upsearch() {
			this.$refs.table.upData(this.search)
		},
        handleKeyDown() {
            this.upsearch()
        },
        // 计算工资
        async table_calc(row){
            let process_data = []
            row.goodsProcess.map(item=>{
                process_data.push({
                    price:item.price,
                    id:item.id,
                    unit:item.unit,
                    process_id:item.process_id,
                    two_process_id:item.two_process_id
                })
            })
            let reqData = {goods_id:row.goods_id,factory_number:row.factory_number,is_suit:row.is_suit,process_data:process_data}
			var res = await this.$API.produce.workshop.calcWages.post(reqData);
			if (res.code == 1) {
                this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", { type: 'error' })
			}
        },
        // 价格记录
        table_record(row){
            this.recordId = row.id
            this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit')
			})
        },
	}
}
</script>

<style lang="scss">
.search-input {
	width: auto;
	font-size: 14px;
}
</style>
